#pragma once
#include<stdio.h>
#include"block_queue.h"
#include<string>
#include<mutex>
#include<string.h>

#include <time.h>
#include <sys/time.h>
#include <stdarg.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
using namespace std;
class BlockLog
{ 
public:
    static BlockLog* getInstance();
    static void flush_log_thread(void *args);


    bool init(const char *file_name, int close_log, int log_buf_size = 8192, int split_lines = 5000000, int max_queue_size = 0);

    void write_log(int level, const char *format, ...);

    void mflush(void);

        int m_close_log; //关闭日志
private:

    void *async_write_log();
    BlockLog();
    ~BlockLog();


private:
    string dir_name; //路径名
    string log_name; //log文件名
    int m_split_lines;  //日志最大行数
    int m_log_buf_size; //日志缓冲区大小
    long long m_count;  //日志行数记录
    int m_today;        //因为按天分类,记录当前时间是那一天
    FILE *m_fp;         //打开log的文件指针
    char *m_buf;
    block_queue<string> *m_log_queue; //阻塞队列
    bool m_is_async;                  //是否同步标志位
    static mutex m_mutex;


};


#define LOG_DEBUG(format, ...) if(0 == BlockLog::getInstance()->m_close_log) {BlockLog::getInstance()->write_log(0, format, ##__VA_ARGS__); BlockLog::getInstance()->mflush();}
#define LOG_INFO(format, ...) if(0 == BlockLog::getInstance()->m_close_log) {BlockLog::getInstance()->write_log(1, format, ##__VA_ARGS__); BlockLog::getInstance()->mflush();}
#define LOG_WARN(format, ...) if(0 == BlockLog::getInstance()->m_close_log) {BlockLog::getInstance()->write_log(2, format, ##__VA_ARGS__); BlockLog::getInstance()->mflush();}
#define LOG_ERROR(format, ...) if(0 == BlockLog::getInstance()->m_close_log) {BlockLog::getInstance()->write_log(3, format, ##__VA_ARGS__); BlockLog::getInstance()->mflush();}
